package com.ostrich.note;

import android.view.View;
import android.view.WindowManager;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.ostrich.note.adapter.MainFragmentPagerAdapter;
import com.ostrich.note.base.BaseActivity;
import com.ostrich.note.databinding.ActivityMainBinding;
import com.ostrich.note.entity.TabEntity;
import com.ostrich.note.ui.calendar.CalendarFragment;
import com.ostrich.note.ui.note.views.NoteAddActivity;
import com.ostrich.note.ui.note.views.NoteFragment;
import com.ostrich.note.ui.settings.views.SettingsFragment;
import com.ostrich.note.utils.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding>{
    private ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();
    private List<Fragment> mFragments = new ArrayList<>();
    private MainFragmentPagerAdapter mMainFragmentPagerAdapter;
    private static final int REQUEST_CODE_PERMISSION = 101;
    @Override
    protected void iniView() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        PermissionUtil.checkPermission(MainActivity.this, new String[]{
                "android.permission.WRITE_EXTERNAL_STORAGE",
                "android.permission.READ_EXTERNAL_STORAGE"
        }, REQUEST_CODE_PERMISSION);
        tabEntitys.add(new TabEntity("笔记",R.mipmap.btn_note,R.mipmap.btn_note_gray));
        tabEntitys.add(new TabEntity("日历",R.mipmap.btn_calendar,R.mipmap.btn_calendar_gray));
        tabEntitys.add(new TabEntity("设置",R.mipmap.btn_setting,R.mipmap.btn_setting_gray));
        binding.common.setTabData(tabEntitys);

        mFragments.add(new NoteFragment());
        mFragments.add(new CalendarFragment());
        mFragments.add(new SettingsFragment());
        mMainFragmentPagerAdapter = new MainFragmentPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mFragments);
        binding.viewPager.setAdapter(mMainFragmentPagerAdapter);



    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        //上下联动
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.common.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        binding.common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }
}